﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace GameBase.Form.Control
{
    public class MyCombobox : MyGroupControl
    {
        //static Texture2D COMBOBOX_TEXTURE = new 
        int segment = 30;
        bool dropDownStyle= true;
        bool isExpanded = false;
        int selectedIndex = -1;
        //public delegate void ItemChangedCallBack(MyControl sender);
        private CallBack itemChangedCallBack = DefaultCallBack;

        public CallBack ItemChangedCallBack
        {
            get { return itemChangedCallBack; }
            set { itemChangedCallBack = value; }
        }
        bool isColorStyle = false;
       
        

        public bool IsColorStyle
        {
          get { return isColorStyle; }
          set { isColorStyle = value; }
        }
        public int SelectedIndex
        {
            get { return selectedIndex; }
            set {
                selectedIndex = value;
                if (selectedIndex < listControls.Count)
                {
                    text = listControls[selectedIndex].Text;
                }
                else
                {
                    selectedIndex = -1;
                }
                ItemChangedCallBack(this);
            }
        }
        public int Segment
        {
            get { return segment; }
            set { segment = value; }
        }
        

        public bool DropDownStyle
        {
            get { return dropDownStyle; }
            set { dropDownStyle = value; }
        }
        public MyCombobox(Vector2 position)
        {
            Sprite = new GameBase.Sprite(@"System/combobox", 3, 1);

            Position = position;
        }
        public MyCombobox( string filePath,Vector2 position)
        {
            Sprite = new GameBase.Sprite(filePath, 3, 1);

            Position = position;
        }
        public override void ControlUpdate(GameTime gameTime)
        {
            base.ControlUpdate(gameTime);

            foreach (MyControl control in listControls)
            {
                //control.Update(
                control.Visible = isExpanded;
            }
        }
        public override void MouseReleased(int x, int y)
        {
            base.MouseReleased(x, y);
            if (isHover)
            {
                isExpanded = !isExpanded;
            }
            
        }
        public override MyControl ControlMouseInput(Microsoft.Xna.Framework.Input.MouseState mouseState)
        {
            
            int i = 0;
            foreach (MyControl control in listControls)
            {
                MyControl focusedControl = control.MouseInput(mouseState);
                if (focusedControl != null)
                {
                    if (mouseState.LeftButton == ButtonState.Released)
                    {
                        SelectItem(control, i);
                    }
                    return focusedControl;
                }
                i++;
            }
            
            if (isHover)
            {
                return this;
            }
            if (mouseState.LeftButton == ButtonState.Released)
            {
                isExpanded = false;
            }
            return null;
        }

        protected virtual void SelectItem(MyControl control, int i)
        {
            if (!control.IsEnable)
                return;
            SelectedIndex = i;
            text = control.Text;
            if (isColorStyle)
            {
                Color = control.Color;
            }
            isExpanded = false;
            //t = 5;
        }
        public override void Add(MyControl control)
        {
            control.ScreenScale = screenScale;
            control.BasePosition = position * screenScale + basePosition;
            control.Depth = depth - 0.0001f;
            listControls.Add(control);
            //float x = position.X;
            float y;
            if(dropDownStyle)
            {
                
                y = sprite.Height-5 + (listControls.Count - 1) * segment;
            }
            else
            {
                y = -listControls.Count * segment;
            }
            control.Position = new Vector2(0, y);
            control.Visible = false;
            //control.Depth = 0.006f;
        }
        public void AddString(string text)
        {
            Add(new MyButton(@"System/comboItem",Vector2.Zero));
        }
        public void AddString(string text, Color color)
        {
            MyButton button = new MyButton(@"System/comboItem", Vector2.Zero,text);
            button.Color = color;
            Add(button);
        }
    }
}
